@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                     <span class="page-title-icon bg-gradient-primary text-white mr-2">
                        <i class="mdi mdi-settings"></i>
                    </span>
                    渠道
                </h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/admin/app/list">应用管理</a></li>
                        <li class="breadcrumb-item active" aria-current="page">渠道列表</li>
                    </ol>
                </nav>
            </div>
            <div class="row">
                <div class="col-lg-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">渠道列表</h4>
                            <div class="col-lg-9" style="float: left;padding: 0;">
                                <button type="button" class="btn btn-sm btn-gradient-success btn-icon-text" onclick="add({{$appId}})">
                                    <i class="mdi mdi-plus btn-icon-prepend"></i>
                                    添加渠道
                                </button>
                            </div>
                            <div class="col-lg-3" style="float: right">
                                <form action="">
                                    <div class="form-group" >
                                        <div class="input-group col-xs-3">
                                            <input type="text" name="wd" class="form-control file-upload-info" placeholder="请输入关键字" value="{{ $wd }}">
                                            <input type="hidden" name="app_id" value="{{ $appId }}">
                                            <span class="input-group-append">
                                                <button class=" btn btn-sm btn-gradient-primary" type="submit"><i class="mdi mdi-account-search btn-icon-prepend"></i>
                                                    搜索
                                                </button>
                                            </span>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <table class="table table-bordered">
                                <thead>
                                <tr>
{{--                                    <th></th>--}}
                                    <th>渠道名称</th>
                                    <th>注册页链接</th>
                                    <th>平台及开关</th>
                                    <th>短链接名称</th>
                                    <th>短链</th>
                                    <th>PV/UV/IP数</th>
{{--                                    <th>创建时间</th>--}}
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($list as $v)
                                    <tr>
{{--                                        <td>{{ $v->id }}</td>--}}
                                        <td>
                                            {{ $v->name }}
                                        </td>
                                        <td>
                                            {{ $v->register_url}}
                                        </td>
                                        <td>
                                            <p><span>微信/QQ:</span><span><input class="mui-switch" type="checkbox"
                                                                               data-channel-id="{{$v->id}}"
                                                                               data-type="weixin_flag" @if($v->weixin_flag) checked @endif></span></p>
                                            <p><span>浏览器:</span><span><input class="mui-switch" type="checkbox"
                                                                             data-channel-id="{{$v->id}}"
                                                                             data-type="brown_flag" @if($v->brown_flag) checked @endif></span></p>
{{--                                            <p><span>分享:</span><span><input class="mui-switch" type="checkbox"--}}
{{--                                                                            data-channel-id="{{$v->id}}"--}}
{{--                                                                            data-type="share_flag" @if($v->share_flag) checked @endif></span></p>--}}
                                        </td>
                                        <td>
                                            {{ $v->short_url_word}}
                                        </td>
                                        <td>
                                            <button class="btn btn-sm btn-primary copy-channel-url" data-clipboard-text="{{ $v->url  }}">
                                                复制链接
                                            </button>
                                        </td>
                                        <td style="text-align: center">
                                            <div style="display: inline-block; border-right: 1px solid gold; padding: 10px;height: 170px;">
                                                <p><b>昨日</b></p>
                                                <p><span>PV:</span><span style="color: red">{{$v->lpvCount}}</span></p>
                                                <p><span>UV:</span><span style="color: red">{{$v->lipCount}}</span></p>
                                                <p><span>IP:</span><span style="color: red">{{$v->lipCount}}</span></p>
                                            </div>
                                            <div style="display: inline-block; padding: 10px;">
                                                <p><b>今日</b></p>
                                                <p><span>PV:</span><span style="color: red">{{$v->pvCount}}</span></p>
                                                <p><span>UV:</span><span style="color: red">{{$v->ipCount}}</span></p>
                                                <p><span>IP:</span><span style="color: red">{{$v->ipCount}}</span></p>
                                            </div>

                                        </td>
{{--                                        <td>{{ $v->created_at }}</td>--}}
                                        <td>
                                            <p>
                                            <button type="button" class="btn btn-sm btn-gradient-dark btn-icon-text" onclick="update({{ $v->id }})">
                                                修改
                                                <i class="mdi mdi-file-check btn-icon-append"></i>
                                            </button>
                                            </p>
                                            <p>

                                            <button type="button" class="btn btn-sm btn-gradient-danger btn-icon-text" onclick="del({{ $v->id }})">
                                                <i class="mdi mdi-delete btn-icon-prepend"></i>
                                                删除
                                            </button>
                                            </p>

                                            <p>
                                                <button type="button" class="btn btn-sm btn-gradient-success btn-icon-text">
                                                    <a href="/admin/access-log/list?channel_id={{$v->id}}&app_id={{$appId}}"><i class="mdi mdi-record-rec btn-icon-append"></i>今日命中({{$v->logCount}})</a>
                                                </button>
                                            </p>
                                            <p>
                                                <a href="/admin/channel/puv/{{$v->id}}">
                                                    <button type="button" class="btn btn-sm btn-gradient-danger btn-icon-text">
                                                        <i class="mdi mdi-fan btn-icon-prepend"></i>
                                                        P(U)V统计
                                                    </button>
                                                </a>
                                            </p>
                                            <p>
                                                <a href="/admin/channel/log-summary?channel_id={{$v->id}}">
                                                    <button type="button" class="btn btn-sm btn-gradient-danger btn-icon-text">
                                                        访问来源统计
                                                    </button>
                                                </a>
                                            </p>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                            <div class="box-footer clearfix">
                                总共 <b>{{ $list->appends(["wd"=>$wd, "app_id" => $appId])->total()  }}</b> 条,分为<b>{{ $list->lastPage() }}</b>页
                                {!! $list->links() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="//cdn.staticfile.org/clipboard.js/2.0.4/clipboard.min.js"></script>
    <script>
        $(function(){
            var clipboard = new ClipboardJS('.btn');

            clipboard.on('success', function(e) {
                layer.msg("链接复制成功!");//弹出提示
                e.clearSelection();
            });

            clipboard.on('error', function(e) {
                layer.msg("链接复制失败，请重试!");//弹出提示
            });

            cutStr(50);

            $('.mui-switch').click(function(){
                var type = $(this).data('type');
                var channelId = $(this).data('channel-id');
                var state = 0;
                if ($(this).prop('checked')) {
                    state = 1;
                }

                $.ajax({
                    url: "/admin/channel/updateFlag",
                    data: {type: type, channelId: channelId, state:state},
                    type: "POST",
                    dataType: "json",
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
                    },
                    success: function(data) {
                        if (data.errcode !=0) {
                            alert('参数错误')
                        }
                        // data = jQuery.parseJSON(data);  //dataType指明了返回数据为json类型，故不需要再反序列化
                    },
                    error: function () {
                        alert('更新失败')
                    }
                });
            })
        });
        function add(app_id){
            var page = layer.open({
                type: 2,
                title: '添加渠道',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '90%'],
                content: '/admin/channel/add?app_id='+app_id
            });
        }
        function update(id){
            var page = layer.open({
                type: 2,
                title: '修改渠道',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '90%'],
                content: '/admin/channel/update/'+id
            });
        }
        function del(id){
            myConfirm("删除操作不可逆,是否继续?",function(){
                myRequest("/admin/channel/del/"+id,"post",{},function(res){
                    layer.msg(res.msg)
                    setTimeout(function(){
                        window.location.reload();
                    },1500)
                });
            });
        }
    </script>
@endsection
